package com.grechur.wanandroidcompose.page


import android.util.Base64
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.grechur.wanandroidcompose.ui.theme.Purple200
import com.grechur.wanandroidcompose.ui.theme.Purple700
import com.grechur.wanandroidcompose.viewmodel.SearchViewModel
import com.grechur.wanandroidcompose.widget.FlowRow
import kotlinx.coroutines.launch
import okhttp3.internal.wait

@Composable
fun CommonWebPage(
    globalNavController: NavHostController,
    searchViewModel: SearchViewModel = viewModel(),
) {
    val appBarHeight = 56.dp
    val statusBarDp = with(LocalDensity.current) {
        WindowInsets.statusBars.getTop(this).toDp()
    }
    val coroutineScope = rememberCoroutineScope()
    val scrollState = rememberScrollState()
    LaunchedEffect(Unit) {
        searchViewModel.friend()
    }
    Scaffold(topBar = {
        TopAppBar(title = {
            Text(text = "常用网站", color = Color.White)
        }, navigationIcon = {
            IconButton(onClick = { globalNavController.popBackStack() }) {
                Icon(
                    imageVector = Icons.Default.ArrowBack,
                    contentDescription = "back",
                    tint = Color.White
                )
            }
        }, modifier = Modifier
            .background(
                Brush.linearGradient(
                    listOf(
                        Purple700, Purple200
                    )
                )
            )
            .height(statusBarDp + appBarHeight)
            .padding(top = statusBarDp),
            backgroundColor = Color.Transparent,
            elevation = 0.dp
        )
    }) {
        Column(modifier = Modifier
            .padding(it)
            .verticalScroll(state = scrollState)) {
            if (searchViewModel.commonList.isSuccess) {
                val commons = searchViewModel.commonList.getOrDefault(listOf())
                FlowRow(modifier = Modifier) {
                    commons.forEach { article ->
                        Button(
                            onClick = {
                                val link = article.link ?: ""
                                val encode =
                                    Base64.encodeToString(link.toByteArray(), Base64.NO_WRAP)

                                globalNavController.navigate("webView?url=${encode}?name=${article.name}")

                            },
                            modifier = Modifier.padding(end = 5.dp),
                            colors = ButtonDefaults.buttonColors(
                                backgroundColor = Color.White,
                                contentColorFor(backgroundColor = Color.Black)
                            )
                        ) {
                            Text(
                                text = "${article.name}",
                                maxLines = 1,
                                overflow = TextOverflow.Ellipsis
                            )
                        }
                    }
                }
            } else {

            }
        }
    }

}

@Preview
@Composable
fun CommonWebPagePreview() {
    CommonWebPage(rememberNavController())
}

